<template>
  <section>
    <div class="container">
      <div class="handle-box">
        <el-input
          v-model="real_name"
          placeholder="用户"
          clearable
          class="handle-input mr10"
          @keyup.enter.native="handleSearch"
        ></el-input>
        <el-input
          v-model="mobile"
          placeholder="手机号"
          clearable
          class="handle-input w200 mr10"
          @keyup.enter.native="handleSearch"
        ></el-input>
        <el-select
          v-model="level"
          placeholder="筛选用户等级"
          clearable
          @keyup.enter.native="handleSearch"
          class="mr10"
        >
          <el-option label="全部" value=""></el-option>
          <el-option
            v-for="item in levelOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
        <el-button type="primary" icon="search" @click="handleSearch"
          >搜索</el-button
        >
        <el-button type="danger" @click="handleReset">清空</el-button>
      </div>
      <el-table
        :data="tableData"
        border
        class="table"
        ref="multipleTable"
        v-loading="loading"
      >
        <el-table-column prop="id" label="ID"></el-table-column>
        <el-table-column label="姓名">
          <template slot-scope="scope">
            <span v-if="scope.row.real_name_status == 1">{{
              scope.row.real_name
            }}</span>
            <span style="color:#f00" v-if="scope.row.real_name_status == 0"
              >未实名</span
            >
          </template>
        </el-table-column>
        <el-table-column prop="mobile" label="电话"></el-table-column>
        <el-table-column label="等级">
          <template slot-scope="scope">
            <el-select
              v-model="scope.row.level"
              placeholder="请选择"
              @change="handleUpdateLevel($event, scope.row.id)"
            >
              <el-option
                v-for="item in levelOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </template>
        </el-table-column>
        <el-table-column prop="cash" label="账户余额"></el-table-column>
        <el-table-column prop="create_time" label="注册时间"></el-table-column>
        <el-table-column prop="recommend_user_real_name" label="推广人">
          <template slot-scope="scope">
            <el-button
              v-if="scope.row.recommend_user_mobile"
              @click="showUpdateFatherDiaLog(scope.row)"
              type="primary"
              icon="el-icon-edit"
              >{{ scope.row.recommend_user_real_name }}({{
                scope.row.recommend_user_mobile
              }})</el-button
            >
            <el-button
              v-if="!scope.row.recommend_user_mobile"
              @click="showUpdateFatherDiaLog(scope.row)"
              type="primary"
              icon="el-icon-edit"
              >没有推广人</el-button
            >
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center">
          <template>
            <el-popover
              placement="bottom"
              title="确定要删除该用户吗？"
              width="200"
              trigger="click"
            >
              <el-row>
                <el-button type="primary">点错了</el-button>
                <el-button type="danger">是的，删除</el-button>
              </el-row>
              <el-button type="danger" slot="reference">删除</el-button>
            </el-popover>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination" style="text-align:center">
        <el-pagination
          background
          @current-change="handleCurrentChange"
          @size-change="handleSizeChange"
          layout="sizes, prev, pager, next,total"
          :total="pagination.total"
          :current-page.sync="pagination.currentPage"
          :page-size="pagination.pageSize"
          :page-sizes="pagination.pageSizes"
          :hide-on-single-page="true"
        >
        </el-pagination>
      </div>
      <el-dialog :title="dailogTitle" :visible.sync="updateFatherVisible" modal>
        <div>
          <div>
            <el-input
              v-model="father_real_name"
              placeholder="用户"
              clearable
              class="handle-input mr10"
              @keyup.enter.native="handleSearch"
            ></el-input>
            <el-input
              v-model="father_mobile"
              placeholder="手机号"
              clearable
              class="handle-input w200 mr10"
              @keyup.enter.native="handleSearch"
            ></el-input>
            <el-button type="primary" icon="search" @click="handleFatherSearch"
              >搜索</el-button
            >
            <el-button type="danger" @click="handleFatherReset">清空</el-button>
          </div>
          <el-table
            v-loading.lock="fatherLoading"
            :data="gridData"
            height="300"
            v-model="updateData"
          >
            <el-table-column property="id" label="ID"></el-table-column>
            <el-table-column
              property="real_name"
              label="姓名"
            ></el-table-column>
            <el-table-column property="mobile" label="手机号"></el-table-column>
            <el-table-column label="操作">
              <template slot-scope="scope">
                <el-button
                  type="warning"
                  v-if="updateData.recommend_user_mobile != scope.row.mobile"
                  @click="handleFather(scope.row)"
                  >修改成该用户下级</el-button
                >
              </template>
            </el-table-column>
          </el-table>
        </div>
      </el-dialog>
    </div>
  </section>
</template>

<script>
import { list, updateUserFather, updateUserLevel } from '@/api/modules/user'
import moment from 'moment'

export default {
  name: 'user',
  components: {},
  data() {
    return {
      loading: true,
      fatherLoading: false,
      dialogVisible: false,
      tableData: [],
      real_name: '',
      mobile: '',
      card_number: '',
      level: '',
      updateFatherVisible: false,
      updateData: [],
      dailogTitle: '',
      father_real_name: '',
      father_mobile: '',
      pagination: {
        total: 0,
        pageSize: 20,
        currentPage: 1,
        pageSizes: [10, 20, 30]
      },
      gridData: [],
      levelOptions: [
        {
          value: 1,
          label: '普通用户'
        },
        {
          value: 2,
          label: '代理商'
        },
        {
          value: 3,
          label: '联合创始人'
        }
      ]
    }
  },
  computed: {},
  created() {
    this.getData()
  },
  methods: {
    search() {
      this.getData()
    },
    // 分页导航
    handleCurrentChange(val) {
      this.pagination.currentPage = val
      this.getData()
    },
    handleSizeChange(val) {
      this.pagination.pageSize = val
      this.getData()
    },
    // 获取数据
    getData() {
      this.loading = true
      list({
        page: this.pagination.currentPage,
        pageSize: this.pagination.pageSize,
        mobile: this.mobile,
        real_name: this.real_name,
        card_number: this.card_number,
        level: this.level
      }).then(res => {
        this.loading = false
        this.tableData = res.data.list
        this.levelOptions = res.data.level
        this.pagination.total = res.data.pagination.total
      })
    },

    recommendFormat(row, column) {
      if (row.recommend_user_mobile) {
        return (
          <el-button type="primary" icon="el-icon-edit">
            {row.recommend_user_real_name}({row.recommend_user_mobile})
          </el-button>
        )
      } else {
        return (
          <el-button type="primary" icon="el-icon-edit">
            没有推广人
          </el-button>
        )
      }
    },

    /**
     * 重置
     */
    handleReset() {
      this.page = 1
      this.pageSize = 20
      this.mobile = ''
      this.real_name = ''
      this.card_number = ''
      this.level = ''
      this.getData()
    },

    handleFatherReset() {
      this.father_mobile = ''
      this.father_real_name = ''
      this.handleFatherSearch()
    },

    /**
     * 搜索
     */
    handleSearch() {
      // this.$router.replace({ query: '' })
      this.getData()
    },

    /**
     * 展示上级列表
     */
    showUpdateFatherDiaLog(row) {
      this.updateData = row
      this.loading = true
      list({}).then(res => {
        if (res.status == 'success') {
          this.gridData = res.data.list
          ;(this.updateFatherVisible = true),
            (this.dailogTitle =
              '修改' + row.real_name + '(' + row.mobile + ')' + '的推广人')
        }
        this.loading = false
      })
    },

    /**
     * 搜索上级
     */
    handleFatherSearch() {
      this.fatherLoading = true
      list({
        mobile: this.father_mobile,
        real_name: this.father_real_name
      }).then(res => {
        if (res.status == 'success') {
          this.gridData = res.data.list
        }
        this.fatherLoading = false
      })
    },

    /**
     * 调整上级
     */
    handleFather(father) {
      this.fatherLoading = true
      updateUserFather({
        user_id: this.updateData.id,
        father_id: father.id
      }).then(res => {
        if (res.status == 'success') {
          for (let i = 0; i < this.tableData.length; i++) {
            if (this.tableData[i].id === this.updateData.id) {
              this.tableData[i].recommend_user_real_name = father.real_name
              this.tableData[i].mobile = father.mobile
            }
          }
          this.$message({
            message: '更新成功',
            type: 'success'
          })
        }
        this.fatherLoading = false
      })
    },
    /**
     * 更新用户等级
     */
    handleUpdateLevel(level, userId) {
      this.loading = true
      updateUserLevel({
        user_id: userId,
        level: level
      }).then(res => {
        if (res.status == 'success') {
          this.$message({
            message: '更新成功',
            type: 'success'
          })
        }
        this.loading = false
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.handle-box {
  margin-bottom: 20px;
}

.handle-select {
  width: 120px;
}

.handle-input {
  width: 100px;
  display: inline-block;
}
.w200 {
  width: 200px;
}

.del-dialog-cnt {
  font-size: 16px;
  text-align: center;
}
.table {
  width: 100%;
  font-size: 14px;
}
.red {
  color: #ff0000;
}
.mr10 {
  margin-right: 10px;
}
</style>
